package com.gitee.hermer.engine.jars.core.eventbus.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import com.gitee.hermer.boot.jee.commons.log.UtilsContext;
import com.gitee.hermer.engine.jars.core.autoconfigure.annotate.ConditionalOnProperty;
import com.gitee.hermer.engine.jars.core.domain.EngineConstant;
import com.gitee.hermer.engine.jars.core.eventbus.EventBusService;
import com.gitee.hermer.engine.jars.core.eventbus.config.EventBusProperties.ModelFactory;
import com.gitee.hermer.engine.jars.core.eventbus.factory.activemq.ActiveMQEventBusService;
import com.gitee.hermer.engine.jars.core.eventbus.factory.activemq.ActiveMQExceptionListener;
import com.gitee.hermer.engine.jars.dependent.activemq.config.ActiveMQProperties;
import com.gitee.hermer.engine.jars.dependent.activemq.configuration.ActiveMQAutoConfiguration;
import com.gitee.hermer.engine.jars.dependent.activemq.core.ActiveMQCluster;
import com.gitee.hermer.engine.jars.dependent.activemq.core.ActiveMQContext;
import com.gitee.hermer.engine.jars.dependent.activemq.core.ActiveMQManager;
import com.gitee.hermer.engine.jars.dependent.dsm.IEnginePropertiesAware;

@Configuration
@ConditionalOnProperty(name=EngineConstant.PROPERTIES_VALUE_KEY_EVENTBUS_MODEL_FACTORY,havingValue=EngineConstant.DEFAULT_VALUES_ENGINE_CONFIG_EVENTBUS_ACTIVEMQ_MODEL_FACTORY)
@Import(ActiveMQAutoConfiguration.class)
public class EventBusActiveMQAutoConfiguration extends UtilsContext implements Ordered,IEnginePropertiesAware<ActiveMQProperties>{
	
	private ActiveMQProperties properties;
	
	
	@Bean
	public EventBusService activeMQEventBusService(){
		info("Eventbus Model -> "+ModelFactory.ActiveMQEvent);
		return new ActiveMQEventBusService();
	}
	
	@Bean
	public ActiveMQExceptionListener activeMQExceptionListener(){
		return new ActiveMQExceptionListener();
	}
	
	@Bean
	public ActiveMQContext activeMQContext(){
		return new ActiveMQContext(properties);
	}
	
	@Bean
	public ActiveMQCluster activeMQCluster(){
		return new ActiveMQCluster(properties.getBrokers());
	}
	
	
	@Bean
	public ActiveMQManager activeMQManager(ActiveMQCluster cluster){
		return new ActiveMQManager(cluster);
	}
	
	

	@Override
	public int getOrder() {
		return HIGHEST_PRECEDENCE + 1000;
	}

	@Override
	@Autowired
	public void setProperties(ActiveMQProperties properties) {
		this.properties = properties;
	}
}
